package com.sgb.kjwl.view.ui.adapter.machine;

import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemAskLeaseBinding;
import com.sgb.kjwl.model.entity.lease.AskLeaseSheBeiEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.DateUtils;

import java.util.List;

public class AskLeaseAdapter extends BaseRecycleAdapter<AskLeaseSheBeiEntity> {

    public AskLeaseAdapter(Context context, List<AskLeaseSheBeiEntity> datas) {
        super(context, R.layout.item_ask_lease, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, AskLeaseSheBeiEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        ItemAskLeaseBinding binding = (ItemAskLeaseBinding) holder.getmBinding();
        if (null != entity) {
            //设置期望租金
            try {
                binding.tvTitle.setText(entity.getRentalTitle());
                if ("1".equals(entity.getNegotiable()) || Double.parseDouble(entity.getRentalPrice()) < 0) {
                    binding.tvSalary.setText("面议");
                } else {
                    binding.tvSalary.setText(entity.getRentalPrice() + "元/" + entity.getRentalDurationUnit());
                }
            } catch (Exception ex) {
                binding.tvSalary.setText("面议");
            }

            //设置租赁时长
            try {
                //发布时间
                binding.tvLeasePublicTime.setText(DateUtils.stampToDay(entity.getUpdateTime()));
                binding.tvEndTime.setText(DateUtils.stampToDate(entity.getEndTime()));
                if (TextUtils.isEmpty(entity.getRentalDuration()) || Double.parseDouble(entity.getRentalDuration()) <= 0) {
                    binding.tvLeaseTime.setText("面议");
                } else {
                    binding.tvLeaseTime.setText(entity.getRentalDuration() + entity.getRentalDurationUnit());
                }
            } catch (Exception ex) {
            }


            //状态(1:报价中;3:报价结束;4:已完成)
            if (entity.getStatus() == 1) {
                binding.ivQuoteState.setImageResource(R.mipmap.price_ing);
            } else if (entity.getStatus() == 3) {
                binding.ivQuoteState.setImageResource(R.mipmap.price_end_gray);
            } else if (entity.getStatus() == 4) {
                binding.ivQuoteState.setImageResource(R.mipmap.price_finish_fail);
            }

            /****************************因接口返回数据字段不一致,需要兼容********************************/

            try {
//                if (!TextUtils.isEmpty(entity.getLocation())) {
//                    binding.tvLeaseAddr.setText(entity.getLocation().replace("/", ""));
//                } else {
//                    binding.tvLeaseAddr.setText(entity.getProvinceName() + entity.getCityName() + entity.getAreaName());
//                }
                if (!TextUtils.isEmpty(entity.getTaskAddr())) {
                    binding.tvLeaseAddr.setText(entity.getTaskAddr());
                } else {
                    if (!TextUtils.isEmpty(entity.getLocation())) {
                        binding.tvLeaseAddr.setText(entity.getLocation().replace("/", ""));
                    } else {
                        binding.tvLeaseAddr.setText(entity.getProvinceName() + entity.getCityName() + entity.getAreaName());
                    }
                }

                binding.tvNumLabel.setText("租赁数量");
//                if (!TextUtils.isEmpty(entity.getRentalCount()) && !TextUtils.isEmpty(entity.getRentalUnit())) {
//                    binding.tvNum.setText(entity.getRentalCount() + entity.getRentalUnit());
//                } else if (!TextUtils.isEmpty(entity.getRentalCount()) && !TextUtils.isEmpty(entity.getRentalCountUnit())) {
//                    binding.tvNum.setText(entity.getRentalCount() + entity.getRentalCountUnit());
//                } else if (!TextUtils.isEmpty(entity.getRentalCount())) {
//                    binding.tvNum.setText(entity.getRentalCount());
//                } else
                if (!TextUtils.isEmpty(entity.getNumOrArea())) {
                    binding.tvNum.setText(entity.getNumOrArea());
                }
            } catch (Exception e) {
            }

            //设置房产,土地,工厂租赁面积
            if ("4".equals(entity.getType()) || "5".equals(entity.getType()) || "6".equals(entity.getType())) {//土地求租
                binding.tvNumLabel.setText("租赁面积");
//                if (!TextUtils.isEmpty(entity.getRentalArea())) {
//                    binding.tvNum.setText(entity.getRentalArea() + " ㎡");
//                }
            }

        }

    }
}
